<template>
	<view class="content">
		<!-- <view class="">
			viw
		</view> -->
		<view class="content-ch">
			<view class="one">
				<view class="one-left">
					<u-upload :fileList="fileList" @afterRead="afterRead" name="file" :maxCount="1" :multiple="false">
						<u-avatar :src="IMG_URL+ user.avatar" size="100"></u-avatar>
					</u-upload>
				</view>
				<view class="one-right">
					<view class="one-top">
						<span v-if="user.username">{{ user.username }}</span>
						<view v-else class="mobile">
							{{user.mobile}}
						</view>
						<span style="margin-left: 15px;" v-if="user.username">{{ user.gender==1?"♂":"♀" }}</span>
					</view>
					<view class="one-bottom">
						{{ user.id_card }}
					</view>
				</view>
				<view class="loginout" @click="logout()">
					登出
				</view>
			</view>
			<u-divider text="为你推荐" textSize="20" textColor="#444" ></u-divider>
			<view class="bottom-box">
				<scroll-view style="height: 100%;" scroll-y="true">
					<view class="" v-for="item in list" @click="goArticle(item.id)">
						<view class="title">
							{{item.title}}
						</view>
						<view class="">
							{{ item.user.username }}发布于{{item.ip}} | {{formatTime(item.createtime)}}
						</view>
						<view class="line">
							<u-line></u-line>
						</view>
					</view>
					<view class="more" @click="more">
						{{moretitle}}
					</view>
				</scroll-view>
			</view>
			
		</view>
	</view>

</template>

<script>
	import {
		IMG_URL,
	} from "@/config/app.js";
	import {
		getInformation,
		uploadImage,
		updateAvatar,
		getArticle
	} from "../../api/my.js";
	import {
		loginOut
	} from "../../api/login.js";
	export default {
		data() {
			return {
				IMG_URL: IMG_URL,
				user: {},
				fileList: [],
				list: [],
				page:1,
				moretitle:'查看更多'
			}
		},
		onLoad() {
			this.getinfo()
			this.getArticles()
		},
		methods: {
			formatTime(timestamp) {
				const date = new Date(timestamp * 1000); // 将秒转换为毫秒
				const year = date.getFullYear();
				const month = (date.getMonth() + 1).toString().padStart(2, '0');
				const day = date.getDate().toString().padStart(2, '0');
				const hours = date.getHours().toString().padStart(2, '0');
				const minutes = date.getMinutes().toString().padStart(2, '0');
				const seconds = date.getSeconds().toString().padStart(2, '0');
				return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
			},
			async getinfo() {
				await getInformation().then(res => {
					console.log('获取个人信息', res);
					this.user = res.data
				});
			},
			async getArticles() {
				await getArticle({
					'page':this.page
				}).then(res => {
					if(res.data.data.length<6){
						
						uni.showToast({
							'title':'没有更多了',
							'icon':'none',
							'duration':1500
						})
						this.moretitle = '没有更多了'
					}else{
						this.list = this.list.concat(res.data.data);
						
					}
					console.log('获取文章', this.list);
				});
			},
			more(){
				this.page++; // 增加页面索引
				this.getArticles(); // 调用获取文章的方法以获取更多文章
			},
			async afterRead(e) {
				const res = await uploadImage(e.file.url)
				let url = res.data.url
				if (res.code == 1) {
					await updateAvatar({
						'avatar': res.data.url
					}).then(res => {
						if (res.code == 1) {
							this.user.avatar = url
						}
					})
				}
			},
			goArticle(id){
				uni.navigateTo({
					url:"/pages/index/detail?id="+id
				})
			},
			async logout(){
				await loginOut().then(res => {
					if (res.code == 1) {
						uni.showToast({
							'icon':'none',
							'duration':1500,
							'title':res.msg
						})
						setTimeout(function() {
						    uni.redirectTo({
						    	url:'/pages/login/login'
						    })
						}, 1500);
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		background-color: #e8f0fe;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-around;
		height: 100vh;
		overflow-y: auto;
		width: 100%;
		.content-ch {
			width: 85%;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			overflow-y: auto;
			.one {
				display: flex;
				justify-content: space-between;
				align-items: center;
				// margin-top: 30px;
				color: #444;
				height: 250rpx;

				.one-right {
					display: flex;
					flex-direction: column;
					margin-right: 50px;
					height: 100%;
					justify-content: space-around;

					.one-top {
						font-size: 20px;
					}
				}
			}
		}
	}
	.bottom-box{
		    height: 60%;
			margin-bottom: 50rpx;
			overflow-y: hidden;
		.title{
			font-size: 15px;
			color: #444;
			    margin-bottom: 15px;
		}
		.line{
			    margin-top: 10px;
				margin-bottom: 15px;
		}
		.more{
			display: flex;
			    justify-content: center;
			    color: #3c9cff;
		}
	}
</style>